import { RoomJinbiConfig } from "../src/config/RoomJinbiConfig";
import { RoomMatchConfig } from "../src/config/RoomMatchConfig";
import { ServerConfig } from "../src/config/ServerConfig";
import { SocketConnectionManager } from "../src/engine/socketserver/socket/SocketConnectionManager";
import { Logger } from "../src/engine/utils/Logger";
import { Md5Helper } from "../src/engine/utils/Md5Helper";
import { WebUtil } from "../src/engine/utils/WebUtil";
import { SystemSettingModel } from "../src/model/SystemSettingModel";

const request = require('request');
const crypto = require("crypto");

const routerMap = {
    updateServerStatus: "/updateServerStatus",
    online: "/online",
    update_system_setting: "/update_system_setting",
    logtest: "/logtest",
    get_server:"/get_server",
}

export class HallServerReqHandler {

    public static instance: HallServerReqHandler = new HallServerReqHandler();

    public init(app) {
        for (const key in routerMap) {
            app.use(routerMap[key], this.onBaseRoot.bind(this))
        }
    }

    private onBaseRoot(req, resp) {
        try {
            let ip: string = WebUtil.getIpByExpressReq(req);
            Logger.log("onBaseRoot==", ip, req.baseUrl)
            if (req.baseUrl == routerMap.updateServerStatus) {
                ServerConfig.initConfig(ServerConfig.nowServerConfig.nowPort)
                resp.end("success");
            }
            else if (req.baseUrl == routerMap.update_system_setting) {
                SystemSettingModel.instance.init()
                resp.end("success");
            }
            else if (req.baseUrl == routerMap.online) {
                let count: number = SocketConnectionManager.instance.getConnectionCount()
                let result: object = {
                    "count": count
                }
                resp.end(JSON.stringify(result));
            }
            else if(req.baseUrl == routerMap.get_server){
                let obj:object = {
                    "upload_server_list":ServerConfig.upload_server_list,
                    "gm_server_list":ServerConfig.gm_server_list,
                    "pay_server_List":ServerConfig.pay_server_List,
                    "WebServer_List":ServerConfig.WebServer_List,
                    "account_server_List":ServerConfig.account_server_List,
                    "hall_server_List":ServerConfig.hall_server_List,
                    "log_server_list":ServerConfig.log_server_list,
                }
                resp.end(JSON.stringify(obj))
            }
            // else if (req.baseUrl == routerMap.onlie_club) {
            //     this.on_onlie_club(req, resp, ip)
            // }
            // else if (req.baseUrl == routerMap.is_in_server) {
            //     this.on_is_in_server(req, resp, ip)
            // }
            // else if (req.baseUrl == routerMap.is_user_online) {
            //     this.on_is_user_online(req, resp, ip)
            // }
        } catch (error) {
            Logger.warn("onBaseRoot error", error);
        }
    }

    private getBody(ip: string, req, resp, callback: Function) {
        // Logger.log("getBody===", req.body)
        // Logger.log("getBody==query=", req.query)
        if (req.body) {
            try {
                // let jsonObj = JSON.parse(req.body);
                // let data: object = jsonObj["data"]
                callback && callback(req, resp, req.body, ip);
            } catch (error) {
                Logger.warn("收到非法内部Http请求body", ip, req.body, error)
                resp.end();
            }
        }
        else {
            resp.end();
        }
    }

    private checkBody(ip: string, req, resp, callback: Function) {
        if (req.body) {
            try {
                let jsonObj = JSON.parse(req.body);
                let k: string = jsonObj["k"];
                let data: object = jsonObj["data"]
                if (k == Md5Helper.getMd5_encrypt(JSON.stringify(data))) {
                    callback && callback(req, resp, data, ip);
                }
                else {
                    Logger.warn("收到非法Http请求 key", ip, req.body)
                    resp.end();
                }
            } catch (error) {
                Logger.warn("收到非法Http请求body", ip, req.body, error)
                resp.end();
            }
        }
        else {
            resp.end();
        }
    }


}